//
//  XJLocationTool.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/30.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJLocationTool.h"
//百度:定位+附近地址
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Search/BMKGeocodeSearch.h>
#import <BaiduMapAPI_Search/BMKPoiSearchType.h>

//百度:模糊搜索
//#import <BaiduMapAPI_Search/BMKSuggestionSearch.h>
#import <BaiduMapAPI_Search/BMKPoiSearch.h> //
@interface XJLocationTool() <BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate,BMKPoiSearchDelegate>

@property(nonatomic,strong)BMKLocationService* locService;
@property(nonatomic,strong)BMKGeoCodeSearch* geocodesearch;
@property(nonatomic,assign)CLLocationCoordinate2D currentCoordinate;

@property(nonatomic, strong)BMKPoiSearch *poiSearch;

@property(nonatomic, strong) NSMutableArray *dataArray;

@end


@implementation XJLocationTool

#pragma mark - 二 单例
+ (XJLocationTool *)shareLocationTool {
   static XJLocationTool *manger = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!manger) {
            manger = [[XJLocationTool alloc] init];
        }
    });
  
    return manger;
}


//开始定位
-(void)startLocation
{
    // 设置的代理
    self.locService.delegate = self;
    self.geocodesearch.delegate = self;
    
    [self.locService startUserLocationService];
}


#pragma mark - 一 lazy
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(BMKLocationService*)locService
{
    if (_locService==nil)
    {
        _locService = [[BMKLocationService alloc]init];
    }
    return _locService;
}


-(BMKGeoCodeSearch*)geocodesearch
{
    if (_geocodesearch==nil)
    {
        _geocodesearch=[[BMKGeoCodeSearch alloc]init];
    }
    return _geocodesearch;
}

// 百度搜索类
- (BMKPoiSearch *)poiSearch{
    if (!_poiSearch) {
        _poiSearch = [[BMKPoiSearch alloc] init];
    }
    return _poiSearch;
}


#pragma mark - methods
-(void)startGeocodesearchWithCoordinate:(CLLocationCoordinate2D)coordinate
{
    //反地理编码
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    reverseGeocodeSearchOption.reverseGeoPoint = coordinate;
    [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];
}

-(void)setCurrentCoordinate:(CLLocationCoordinate2D)currentCoordinate
{
    _currentCoordinate=currentCoordinate;
    [self startGeocodesearchWithCoordinate:currentCoordinate];
}

#pragma mark - BMKGeoCodeSearchDelegate

/**
 *返回反地理编码搜索结果
 *@param searcher 搜索对象
 *@param result 搜索结果
 *@param error 错误号，@see BMKSearchErrorCode
 */

- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR)
    {
        [self.dataArray removeAllObjects];
        [self.dataArray addObjectsFromArray:result.poiList];
        
        //#import <BaiduMapAPI_Search/BMKPoiSearch.h>
        //将数组中的第一 BMKPoiInfo  一个模型传递出去。
        XJWeakSelf(self);
        if (self.locationToolBlock) {
            weakself.locationToolBlock([weakself.dataArray firstObject]);
        }
    }
}

#pragma mark - 百度定位代理方法
/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    self.currentCoordinate=userLocation.location.coordinate;
    
    if (self.currentCoordinate.latitude!=0)
    {
        [self.locService stopUserLocationService];
    }
}


- (void)didFailToLocateUserWithError:(NSError *)error {
    XJLog(@"定位失败:%@",error);
}

@end

